# __author__ = "James Zhang"
# !/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta, tzinfo
import pytz

from core.conf import settings

ZERO = timedelta(0)


class UTC(tzinfo):
    def __repr__(self):
        return "<UTC>"

    def utcoffset(self, dt):
        return ZERO

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return ZERO

utc = pytz.utc if pytz else UTC()

USE_TZ = settings.USE_TZ


def now():
    if USE_TZ:
        return datetime.utcnow().replace(tzinfo=utc)
    else:
        return datetime.now()


def is_aware(value):
    """
    Determines if a given datetime.datetime is aware.

    The logic is described in Python's docs:
    http://docs.python.org/library/datetime.html#datetime.tzinfo
    :param value:
    """
    return value.tzinfo is not None and value.tzinfo.utcoffset(value) is not None
